package cn.skylark.wcm.service.impl;

import cn.skylark.wcm.dao.ResourcesDao;
import cn.skylark.wcm.dao.RoleDao;
import cn.skylark.wcm.dao.RoleResourcesDao;
import cn.skylark.wcm.dao.UserRoleDao;
import cn.skylark.wcm.entity.Resources;
import cn.skylark.wcm.entity.Role;
import cn.skylark.wcm.entity.RoleResources;
import cn.skylark.wcm.service.RoleResourcesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色权限服务实装serviceImpl
 *
 * @author chuhl
 * @since
 */
@Service("roleResourcesService")
public class RoleResourcesServiceImpl implements RoleResourcesService {

    Logger logger = LoggerFactory.getLogger(RoleResourcesServiceImpl.class);
    @Autowired
    public RoleResourcesDao roleResourcesDao;

    @Autowired
    public UserRoleDao userRoleDao;

    @Autowired
    public RoleDao roleDao;

    @Autowired
    public ResourcesDao resourcesDao;


    /**
     * 追加权限
     *
     * @param roleid     角色id
     * @param resourceid 权限数组id
     */
    @Override
    public void addRoleResources(Integer roleid, String[] resourceid) {
        // 删除
        Example example = new Example(RoleResources.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleid", roleid);
        roleResourcesDao.deleteByExample(example);
        // 添加
        for (int i = 0; i < resourceid.length; i++) {
            RoleResources r = new RoleResources();
            r.setRoleid(roleid);
            r.setResourcesid(resourceid[i]);
            r.setDelflg(0);
            roleResourcesDao.insert(r);
        }

    }


    /**
     * 获取所有角色及权限
     * 1;根据租户ID取得用户角色列表
     * 2:根据角色ID取得角色权限列表
     * 3:根据权限ID获取权限内容
     * 4:根据以上1,2,3取得的信息，生成角色权限对应MAP
     * 5:将结果设定到返回值
     *
     * @param tenanid 租户ID
     * @return 返回角色权限MAP
     */
    @Override
    public Map<String, Object> getRoleResources(Integer tenanid) {
        logger.info("根据租户ID获取角色权限MAP--tenanid=" + tenanid);
        Map<String, Object> ret = new HashMap<>();
        Map<String, Object> resourcesMap = new HashMap<>();
        /**
         * 获取角色列表
         */
        Example example = new Example(Role.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("tenanid", tenanid);
        criteria.andEqualTo("delflg", 0);
        List<Role> roleList = roleDao.selectByExample(example);
        String strid = "0";
        String[] strRole = new String[roleList.size()];
        int roleCount = 0;
        for (Role role : roleList) {
            strid = role.getId() + "," + strid;
            strRole[roleCount] = role.getTenanid() + role.getRoledesc();
            roleCount++;
        }
        /**
         * 角色对应的权限ID
         */
        Example exampleRoleResources = new Example(RoleResources.class);
        Example.Criteria criteriaRR = exampleRoleResources.createCriteria();
        criteriaRR.andCondition("roleid in (" + strid + ")");
        criteriaRR.andEqualTo("delflg", 0);
        List<RoleResources> roleResourcesList = roleResourcesDao.selectByExample(exampleRoleResources);
        String strrsourcesid = "0";
        Map<Integer, String> integerStringMap = new HashMap<>();
        String tmpstr = "";
        for (int i = 0; i < roleResourcesList.size(); i++) {
            Resources resourcesCon = new Resources();
            resourcesCon.setId(Integer.valueOf(roleResourcesList.get(i).getResourcesid()));
            resourcesCon.setDelflg(0);
            Resources resources = resourcesDao.selectOne(resourcesCon);
            if (i == 0) {
                tmpstr = resources.getResurl() + ",";
            } else {
                if (!roleResourcesList.get(i - 1).getRoleid().equals(roleResourcesList.get(i).getRoleid())) {
                    integerStringMap.put(roleResourcesList.get(i - 1).getRoleid(), tmpstr);
                    tmpstr = "" + resources.getResurl() + ",";
                } else {

                    tmpstr = "" + resources.getResurl() + "," + tmpstr;
                }
                if (i == roleResourcesList.size() - 1) {
                    integerStringMap.put(roleResourcesList.get(i).getRoleid(), tmpstr);
                }
            }
        }
        for (Role role : roleList) {
            if(integerStringMap.get(role.getId()) != null) {
                resourcesMap.put( role.getTenanid() + role.getRoledesc(), integerStringMap.get( role.getId() ) );
            }
        }
        ret.put("roles", strRole);
        ret.put("permissions", resourcesMap);
        logger.info("根据租户ID获取角色权限MAP--结束");
        return ret;
    }

    /**
     * 通过角色id和权限id查询该角色是否拥有该权限
     *
     * @param id     权限id
     * @param roleid 角色id
     * @return
     */
    @Override
    public int selectRoleResource(Integer id, Integer roleid) {
        RoleResources roleResources = new RoleResources();
        roleResources.setDelflg(0);
        roleResources.setRoleid(roleid);
        roleResources.setResourcesid(id.toString());
        int i = roleResourcesDao.selectCount(roleResources);
        return i;
    }
}
